package com.cyj.dream.admin.persistence.manager.systems;

import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.cyj.dream.admin.model.system.SysRolePermission;
import com.cyj.dream.admin.persistence.service.ITbSysRolePermissionService;
import com.cyj.dream.admin.util.IsEmptyUtils;
import com.cyj.dream.core.constant.enums.ResultCode;
import com.cyj.dream.core.exception.BizException;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.ArrayList;
import java.util.List;

/**
 * @Description: 角色权限关联关系管理器
 * @BelongsProject: dream-DreamChardonnay
 * @BelongsPackage: com.dream.DreamChardonnay.persistence.manager.systems
 * @Author: ChenYongJia
 * @CreateTime: 2021-05-17 11:52
 * @Email: chen87647213@163.com
 * @Version: 1.0
 */
@Slf4j
@Component
public class SysRolePermissionManager {

    @Autowired
    private ITbSysRolePermissionService iTbSysRolePermissionService;

    /**
     * 查询角色权限关联关系表信息
     *
     * @param sysRolePermission 角色权限关联关系对象
     * @return java.util.List<com.dream.DreamChardonnay.model.system.SysRolePermission>
     * @date 2021/5/17 15:29
     * @author ChenYongJia
     * @version 1.0
     */
    public List<SysRolePermission> selectSysRolePermission(SysRolePermission sysRolePermission) {
        log.info("进入 SysRolePermissionManager 方法管理器，参数：{}", JSONObject.toJSONString(sysRolePermission));
        LambdaQueryWrapper<SysRolePermission> wrapper = new LambdaQueryWrapper<>();
        if (!IsEmptyUtils.isEmpty(sysRolePermission.getSysRoleId())) {
            wrapper.eq(SysRolePermission::getSysRoleId, sysRolePermission.getSysRoleId());
        }
        if (!IsEmptyUtils.isEmpty(sysRolePermission.getSysPermissionId())) {
            wrapper.eq(SysRolePermission::getSysPermissionId, sysRolePermission.getSysPermissionId());
        }

        return iTbSysRolePermissionService.list(wrapper);
    }

    /**
     * 为角色设置权限
     *
     * @param sysRoleId       角色id
     * @param sysPermissionId 权限id集合
     * @return boolean
     * @date 2021/5/17 14:46
     * @author ChenYongJia
     * @version 1.0
     */
    public boolean settingSysRolePermission(Long sysRoleId, List<Long> sysPermissionId) {
        log.info("进入 SysRolePermissionManager 为角色设置权限方法管理器，角色id：{}，权限id集合：{}", sysRoleId, sysPermissionId);
        if (!IsEmptyUtils.isEmpty(sysRoleId) && !IsEmptyUtils.isEmpty(sysPermissionId)) {
            List<SysRolePermission> sysRolePermissionList = new ArrayList<>();
            SysRolePermission sysRolePermission = new SysRolePermission();
            sysPermissionId.stream().forEach(item -> {
                sysRolePermission.setSysRoleId(sysRoleId);
                sysRolePermission.setSysPermissionId(item);
                sysRolePermissionList.add(sysRolePermission);
            });

            try {
                return iTbSysRolePermissionService.saveBatch(sysRolePermissionList);
            } catch (Exception e) {
                log.error("为角色设置权限调用批量添加失败，错误信息为：{}", e);
                return false;
            }
        } else {
            log.error("出现错误：方法执行失败失败！参数为空，请检查参数后重新提交！");
            throw new BizException(ResultCode.PARAM_NOT_COMPLETE);
        }
    }

    /**
     * 为角色移除权限
     *
     * @param sysRoleId       角色id
     * @param sysPermissionId 权限id集合
     * @return boolean
     * @date 2021-5-17 14:55:26
     * @author ChenYongJia
     * @version 1.0
     */
    public boolean deleteSysRolePermission(Long sysRoleId, List<Long> sysPermissionId) {
        log.info("进入 SysRolePermissionManager 为角色移除权限方法管理器，角色id：{}，权限id集合：{}", sysRoleId, sysPermissionId);
        LambdaQueryWrapper<SysRolePermission> wrapper = new LambdaQueryWrapper<>();
        if (!IsEmptyUtils.isEmpty(sysRoleId) && !IsEmptyUtils.isEmpty(sysPermissionId)) {
            sysPermissionId.stream().forEach(item -> {
                wrapper.eq(SysRolePermission::getSysRoleId, sysRoleId);
                wrapper.eq(SysRolePermission::getSysPermissionId, item);

                try {
                    iTbSysRolePermissionService.remove(wrapper);
                } catch (Exception e) {
                    log.error("为角色移除权限方法调用删除失败，错误信息为：{}", e);
                }
            });
            return true;
        } else {
            log.error("出现错误：方法执行失败失败！参数为空，请检查参数后重新提交！");
            throw new BizException(ResultCode.PARAM_NOT_COMPLETE);
        }
    }

}
